********************************************************************************
**	PURPOSE: Drop 6 month endline in T6
**							
**	INPUTS: Long with outcomes
**	
**	OUTPUTS: Credit Behaviors_post_avg_effects_flag_randomized_2
**			 Credit Behaviors_post_het_effects_flag_randomized_2
**
**  CREATED/EDITED BY: Kayla Wilding, Leah Kim
**
**	DATE CREATED: 7/2018
**
**	DATE LAST EDITED: 1/30/2018
********************************************************************************

/*******************************************************************************
0. Setup 
********************************************************************************/
clear all

*Filename and table title
local filename1  "Credit Behaviors_post_het groups"
local filename1a "Credit Behaviors_post_avg effects"

*Define the covariate regression spec options and the outcome variables of interest
local outcomes  z_newcredit_i z_default_index_all_i //outcomes 
local decs 3		//decimals 
local lowter ltca	//lower tercile of installment credit activity at baseline index
local midter mtca	//middle tercile of installment credit activity at baseline index 
local highter htca	//upper tercile of installment credit activity at baseline index

*Spec for average treatment effects 
*By post 
local loanspec1a post 1.enc#1.post	
*Spec for heterogeneous treatment effect by post 
*By post 
local loanspec1h 1.`lowter'#1.post 1.`midter'#1.post 1.`highter'#1.post 1.`lowter'#1.enc#1.post 1.`midter'#1.enc#1.post 1.`highter'#1.enc#1.post 

/*******************************************************************************
1. Table 6 Column 1b, 2b
********************************************************************************/
*Import data
use "$adta/Long with outcomes.dta", clear
drop if index == 6 // Drop 6-month endline, change from do file 07. 

*Regression loop with output 
loc j 1
local wordcount = 1
foreach regvar in `outcomes' {
	foreach restrict in "flag_randomized"  {
		cap drop __*
		local wordtot : word count `outcomes'
		areg `regvar' `loanspec`j'a' if `restrict' == 1 /* & `=cond("`wordcount'" == "`wordtot'", "op18", "`restrict'")' == 1 */, absorb(surveyid) vce(cluster surveyid)
		local inds = e(df_a) + 1
		local obs = e(N)
		sum `regvar' if e(sample) == 1 & post == 1 & flag_extragroup == 1
		if r(mean) >= -0.001 & r(mean) <= 0.00 {
			local mu_lhs_post = string(abs(r(mean)), "%14.`decs'f") 
		}  //end if 
		else {
			local mu_lhs_post = string(r(mean), "%14.`decs'f") 
		}           
		sum `regvar' if e(sample) == 1 & index == 12 & flag_extragroup == 1
		if r(mean) >= -0.001 & r(mean) <= 0.00 {
			local mu_lhs_12 = string(abs(r(mean)), "%14.`decs'f") 
		}  //end if 
		else {
			local mu_lhs_12 = string(r(mean), "%14.`decs'f") 
		} 
		sum `regvar' if e(sample) == 1 & index == 18 & flag_extragroup == 1
		if r(mean) >= -0.001 & r(mean) <= 0.00 {
			local mu_lhs_18 = string(abs(r(mean)), "%14.`decs'f") 
		}  //end if 
		else {
			local mu_lhs_18 = string(r(mean), "%14.`decs'f") 
		}        
		sum `regvar' if e(sample) == 1 & index == 1 & flag_extragroup == 1
		if r(mean) >= -0.001 & r(mean) <= 0.00 {
			local mu_lhs = string(abs(r(mean)), "%14.`decs'f") 
		} 	//end if 
		else {
			local mu_lhs = string(r(mean), "%14.`decs'f") 
		}                     
		if "`regvar'" == "`: word 1 of `outcomes''" {
			local action = "replace"
		}   //end regvar ==
		else {
			local action = "append"
		}       
		#delimit ;                              
		outreg2 using "$outputtables/`filename`j'a'_`restrict'_2.xls", `action' excel label
			nocons noobs dec(`decs') nor2 addtext(Obs, `obs', Individuals, `inds', 
			Mean Dependent Variable in Extra Step Group at Baseline, `mu_lhs', 
			Mean Dep Var In Extra Step at Post, `mu_lhs_post', 
			Mean Dep Var In Extra Step at 12, `mu_lhs_12', 
			Mean Dep Var In Extra Step at 18, `mu_lhs_18')  
			slow(8000) nonote noaster;
		#delimit cr;
		sleep 2000 
		cap drop __*
				areg `regvar' `loanspec`j'h' if `restrict' == 1 /* & `=cond("`wordcount'" == "`wordtot'", "op18", "`restrict'")' == 1 */ , absorb(surveyid) vce(cluster surveyid)
		local obs = e(N)
		local inds = e(df_a) + 1
		sum `regvar' if e(sample) == 1 & index == 1 & flag_extragroup == 1
		if r(mean) >= -0.001 & r(mean) <= 0.00 {
			local mu_lhs = string(abs(r(mean)), "%14.`decs'f") 
		} 	 //end if 
		else {
			local mu_lhs = string(r(mean), "%14.`decs'f") 
		}  // end else
		if `j' == 1 {
				di "j `j'"
				test 1.`lowter'#1.enc#1.post = 1.`midter'#1.enc#1.post
				local pvalue1_2 = string(r(p), "%14.`decs'f")
				test 1.`midter'#1.enc#1.post = 1.`highter'#1.enc#1.post
				local pvalue2_3 = string(r(p), "%14.`decs'f")
				test 1.`lowter'#1.enc#1.post = 1.`highter'#1.enc#1.post
				local pvalue1_3 = string(r(p), "%14.`decs'f")
				
				if "`regvar'" == "`: word 1 of `outcomes''" {
					local action = "replace"
				} 	 //end regvar == 
				else {
					local action = "append"
				}  	//end else  
				
				#delimit ;                              
				outreg2 using "$outputtables/`filename`j''_`restrict'_2.xls", `action' excel label 
					nocons noobs dec(`decs') nor2 
					addtext(P-value of 1.`lowter'#1.enc#1.post = 1.`midter'#1.enc#1.post, `pvalue1_2',
							P-value of 1.`midter'#1.enc#1.post = 1.`highter'#1.enc#1.post, `pvalue2_3', 
							P-value of 1.`lowter'#1.enc#1.post = 1.`highter'#1.enc#1.post, `pvalue1_3',
							Obs, `obs', Individuals, `inds', Mean Dependent Variable in Extra Step Group at Baseline, `mu_lhs')  
					slow(8000) nonote noaster;
				#delimit cr;
				sleep 2000 
				//output a blank column
				outreg2 using "$outputtables/`filename`j''_`restrict'_2.xls", append skip
			}  //end j == 1
	}	//end restrict
		local ++wordcount
} //ends regvar

**EOF**
